$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	check();
	//显示用户名
	showUserName();
}


function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchGoodRepairShowInfoByGoodRepairId").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	
	$('#updateGoodRepairModal').on('hide.bs.modal', function (){
		$("#upateGoodRepairId").val("");
		$("#upateReqairDescr").val("");
		$("#upateGoodRepairId-error").remove();
		$("#upateReqairDescr-error").remove();
	});
	
	$('#getStaffInfoModal').on('hide.bs.modal', function (){
		$("#getStaffId").val("");
		$("#getStaffName").val("");
		$("#getStaffBirthday").val("");
		$("#getStaffSex").val("");
		$("#getStaffTel").val("");
		$("#getStaffId-error").remove();
		$("#getStaffName-error").remove();
		$("#getStaffBirthday-error").remove();
		$("#getStaffSex-error").remove();
		$("#getStaffTel-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
}

function check(){
	updateCheck();
	updateInfoCheck();
	updatePasswordCheck();
}


function updateCheck(){
	//修改校验
	$("#updateGoodRepair").validate({
		rules:{
			upateRepairDescr:{
				required:true,
			},
			upateGoodRepairId:{
				required:true,
			}
		},
		messages:{
			upateRepairDescr:{
				required:"请输入维修情况",
			},
			upateGoodRepairId:{
				required:"请输入编号",
			}
		},
		submitHandler:function(form){
			var repairDescr = $("#upateRepairDescr").val();
			var goodRepairId = $("#upateGoodRepairId").val();
			$.ajax({
				type:'post',
				url:'updateGoodRepairByStaff',
				data:{"repairDescr":repairDescr,"goodRepairId":goodRepairId},
				success:function(data){
					queryUserInfo(window.currentpage);
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updateGoodRepairModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateInfoCheck(){
	$("#getStaffInfo").validate({
		rules:{
			getStaffName:{
				required:true
			},
			getStaffSex:{
				required:true
			}
		},
		messages:{
			getStaffName:{
				required:"请输入姓名"
			},
			getStaffSex:{
				required:"请选择性别"
			}
		},
		submitHandler:function(form){
			var staffId = $("#getStaffId").val();
			var staffName = $("#getStaffName").val();
			var staffSex = $("#getStaffSex").val();
			var staffBirthday = new Date($("#getStaffBirthday").val());
			var staffTel = $("#getStaffTel").val();
			$.ajax({
				type:'post',
				url:'updateStaffInfo',
				data:{"staffId":staffId,"staffName":staffName,"staffSex":staffSex,"staffBirthday":staffBirthday,"staffTel":staffTel},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#getStaffInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var goodRepairId = $("#goodRepairId").val();
	$.ajax({
		url:"getGoodRepairShowInfoByStaff",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"goodRepairId":goodRepairId},
        success:function(data){
        	console.log(data);
        	var status = ["未受理","正在受理","受理完成"];
        	$("#goodRepairShowInfoList").empty();
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td>"+ data.list[i].goodRepairId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].dormLocationName + "-" + data.list[i].dormName +"</td>");
        		 trTemp.append("<td>"+ data.list[i].goodName + "-" + data.list[i].goodLocation  +"</td>");
        		 trTemp.append("<td>"+ moment(data.list[i].requestTime).format('L') +"</td>");
        		 trTemp.append("<td>"+ data.list[i].requsetDescr  +"</td>");
        		 trTemp.append("<td>"+ status[data.list[i].repairStatus-1]  +"</td>");
        		 if(data.list[i].repairStatus == 1){
        			 trTemp.append("<td><button type='button' class='btn btn-outline-success btn-sm' onclick='updateGoodRepairStatus(this)'>接受</button></td>");
        		 }
        		 if(data.list[i].repairStatus == 2){
        			 trTemp.append("<td><button type='button' class='btn btn-outline-success btn-sm' onclick='updateDormAdminInfo(this)'>维修完成</button></td>");
        		 }
        		 if(data.list[i].repairStatus == 3){
        			 trTemp.append("<td><button type='button' class='btn btn-outline-success btn-sm' onclick='getDormAdminInfo(this)'>查看</button></td>");
        		 }
        		 trTemp.appendTo("#goodRepairShowInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}

function updateGoodRepairStatus(obj){
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"updateGoodRepairStatusByStaff",
		type:"get",
		data:{'goodRepairId':dormAdmininfo},
		success:function(data){
			queryUserInfo(window.currentpage);
		}
	});
}

function updateDormAdminInfo(obj){
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"getGoodRepairInfo",
		type:"get",
		data:{'goodRepairId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#upateGoodRepairId").val(dormAdmininfo);
		}
	});
	$('#updateGoodRepairModal').modal('show');
}

//获取当前行数据(查看)
function getDormAdminInfo(obj) {
	var x = $(obj).parent().parent().find("td");
	var dormAdmininfo = x.eq(0).text();
	$.ajax({
		url:"getGoodRepairInfo",
		type:"get",
		data:{'goodRepairId':dormAdmininfo},
		success:function(data){
			console.log(data);
			$("#getStuName").val(data.stuInfo.stuName);
			$("#getGoodName").val(data.goodInfo.goodName + "-" + data.goodInfo.goodLocation);
			$("#getRequsetDescr").val(data.goodRepair.requsetDescr);
			if(data.staffInfo != null){
				$("#getStaffNmae").val(data.staffInfo.staffName);
			}else{
				$("#getStaffNmae").val("");
			}
			if(data.goodRepair.repairDescr != null){
				$("#getRepairDescr").val(data.goodRepair.repairDescr);
			}else{
				$("#getRepairDescr").val("");
			}	
			$('#getGoodRepairModal').modal('show');
		}
	});
}

function showStaffInfoModal() {
	$.ajax({
		url:"getStaffInfo",
		type:"get",
		success:function(data){
			console.log(data);
			$("#getStaffId").val(data.staffId);
			$("#getStaffName").val(data.staffName);
			$("#getStaffSex").val(data.staffSex);
			if(data.staffBirthday!=null){
				$("#getStaffBirthday").val(moment(data.staffBirthday).format('L'));
			}else{
				$("#getStaffBirthday").val("");
			}
			$("#getStaffTel").val(data.staffTel);
			$('#getStaffInfoModal').modal('show');
		}
	});
}

function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getStaffInfo",
		type:"get",
		success:function(data){
			$("#userName").text(data.staffName);
		}
	});
}


